﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QL_DTTN.DTO.DTO;
using QL_DTTN.DTO;
namespace QL_DTTN
{
    public partial class FormDangNhap : Form
    {
        public delegate void SEND(TaiKhoan taiKhoan);
        public SEND sender;
        public bool isLogin = false;

        public FormDangNhap()
        {
            InitializeComponent();
           
        }        
        private void FormDangNhap_Load(object sender, EventArgs e)
        {
            txtTenDangNhap.Focus();
        }
        private void tsmData_Click(object sender, EventArgs e)
        {
            KetNoiDatabase frmKetNoi = new KetNoiDatabase();
            frmKetNoi._reconnect = true;
            frmKetNoi.ShowDialog();
        }
        private void btnDangNhap_Click(object sender, EventArgs e)
        {
            if (txtTenDangNhap.Text=="" || txtMatKhau.Text=="")
            {
                isLogin = false;
                MessageBox.Show("Chưa nhập đủ thông tin.");
                return;
            }
            TaiKhoan taiKhoan=new TaiKhoan();
            string err= BUS.Bus_DangNhap.KiemTraDangNhap(txtTenDangNhap.Text.Trim(),Functions.md5(txtMatKhau.Text.Trim()), ref taiKhoan);
 
            if (err=="")
            {
                this.sender(taiKhoan);
                isLogin = true;
                this.Close();
            }
            else
            {
                MessageBox.Show(err);
                isLogin = false;
            }
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            this.Close();
            Application.Exit();
        }

        private void FormDangNhap_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (!isLogin)
                Application.Exit();
        }



    
    }
}
